const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            // numberToChinese,
            house_room: [],
            house_unit: [],
            dataForm: {
                Coords: '',
                Unit_Quantity: '',
                Layer_Quantity: '',
                Room_Quantity: ''
            },
            Grid: [],
            house_type: [],
            personal: [],
            relation: [],
            childDataForm1: [{}],
            natural_resources_management: [],
            cityValue: ['230000', '232700', '232723'],
            cityValue1: [],
            city: [],
            city1: [],
            village_collective: [],
            is: true,
            Blue_Plate_SerialNumber: '',
            House_Room_SerialNumber: '',
            dataForm1: {},
            Layer: {},
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.Blue_Plate_SerialNumber = getQueryObject(ls_URL, "Blue_Plate_SerialNumber");// 获取 nodeKey 参数
        this.House_Room_SerialNumber = getQueryObject(ls_URL, "House_Room_SerialNumber");   // 获取 action 参数
        this.getCategory();
        this.getCity();
        this.getData();
    },
    watch: {
        'dataForm.Village_SerialNumber'(val) {
            if (this.action == 'Add' && this.is) {
                this.is = false;
                let str = this.dataForm.Village_SerialNumber
                if (str.substring(0, 6)) {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/clientOperation/getDistrictTree`,
                        type: "GET",
                        data: {
                            DistrictCode: str.substring(0, 6)
                        },
                        success: (data) => {
                            this.city1 = data[0].children;
                            this.cityValue1 = [str.substring(0, 9), str];
                            this.cityValue = [str.substring(0, 2) + '0000', str.substring(0, 4) + '00', str.substring(0, 6)]
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                }

            }
        },
        'dataForm.Unit_Quantity'(val) {
            if (!isNaN(val)) {
                let obj = deepClone(this.dataForm1);
                this.dataForm1 = {};
                for (let i = 1; i <= Number(val); i++) {
                    if (obj[i]) {
                        this.dataForm1[i] = obj[i];
                    } else {
                        this.dataForm1[i] = '';
                    }
                }
            }
        },
        'dataForm.Layer_JSON'(val) {
            try {
                this.Layer = JSON.parse(val);
            } catch (error) {

            }
        }
    },
    computed: {
        RoomNumbers() {
            const { Unit_Quantity, Layer_Quantity, Room_Quantity, Self_Built_Houses } = this.dataForm;
            if (Self_Built_Houses == '0' && !isNaN(Unit_Quantity) && !isNaN(Layer_Quantity) && !isNaN(Room_Quantity)) {
                let arr = [];
                for (let i = 1; i <= Number(Unit_Quantity); i++) {
                    for (let j = 1; j <= Number(Layer_Quantity); j++) {
                        for (let z = 1; z <= Number(Room_Quantity); z++) {
                            arr.push({
                                House_Room_SerialNumber: this.Blue_Plate_SerialNumber + '-' + i + '-' + (j < 10 ? '0' + j : j) + (z < 10 ? '0' + z : z),
                                Blue_Plate_SerialNumber: this.Blue_Plate_SerialNumber,
                                Unit_Number: i,
                                Layer_Number: j,
                                Room_Number: z,
                                Room_SerialNumber: i + '-' + (j < 10 ? '0' + j : j) + (z < 10 ? '0' + z : z),
                            })
                        }
                    }
                }
                return arr;
            } else if (Self_Built_Houses == '1' && !isNaN(Unit_Quantity)) {
                let arr = [];
                for (let i = 1; i <= Number(Unit_Quantity); i++) {
                    const Layer = this.Layer[i];
                    if (Layer) {
                        const Real_Rstate_Number = Layer.Real_Rstate_Number;
                        for (let j = 1; j <= Number(Layer.Layer_Quantity); j++) {
                            const Room = Layer.Room[j]
                            if (Room) {
                                for (let z = 1; z <= Number(Room.Room_Quantity); z++) {
                                    arr.push({
                                        House_Room_SerialNumber: this.Blue_Plate_SerialNumber + '-' + i + '-' + (j < 10 ? '0' + j : j) + (z < 10 ? '0' + z : z),
                                        Blue_Plate_SerialNumber: this.Blue_Plate_SerialNumber,
                                        Unit_Number: i,
                                        Layer_Number: j,
                                        Room_Number: z,
                                        Room_SerialNumber: i + '-' + (j < 10 ? '0' + j : j) + (z < 10 ? '0' + z : z),
                                        Real_Rstate_Number
                                    })
                                }
                            }
                        }
                    }
                }
                return arr;
            }
            return []
        },
        UnitNumbers() {
            const { Unit_Quantity, Layer_Quantity, Room_Quantity, Self_Built_Houses } = this.dataForm;
            if (Self_Built_Houses == '0' && !isNaN(Unit_Quantity) && !isNaN(Layer_Quantity) && !isNaN(Room_Quantity)) {
                let arr = [];
                for (let i = 1; i <= Number(Unit_Quantity); i++) {
                    arr.push({
                        House_Unit_SerialNumber: this.Blue_Plate_SerialNumber + '-' + i,
                        Blue_Plate_SerialNumber: this.Blue_Plate_SerialNumber,
                        Unit_Number: i,
                        Responsible_Person: this.dataForm1[i]
                    })
                }
                return arr;
            }
            return [];
        },
        liveStatus() {
            return function (Unit_Number, Layer_Number, Room_Number) {
                return this.house_room.some(i => i.Unit_Number == Unit_Number && i.Layer_Number == Layer_Number && i.Room_Number == Room_Number && i.Live_Status == '1')
            }
        }
    },
    methods: {
        Input_Unit_Quantity(val) {
            if (this.dataForm.Self_Built_Houses == '1' && !isNaN(Number(val))) {
                this.Layer = {};
                for (let i = 1; i <= Number(val); i++) {
                    this.Layer[i] = { Layer_Quantity: '', Room: {}, Real_Rstate_Number: '' };
                }
            }
        },
        Input_Layer_Quantity(val, item) {
            if (this.dataForm.Self_Built_Houses == '1' && !isNaN(Number(val))) {
                item.Room = {};
                for (let i = 1; i <= Number(val); i++) {
                    item.Room[i] = { Room_Quantity: '' };
                }
            }
        },
        getData() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: "POST",
                data: {
                    Data: 'blue_plate_information',
                    Condition: "Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "'",
                },
                success: (data) => {
                    this.dataForm = data[0];
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        changeCity1(val) {
            if (val.length > 0) {
                this.dataForm.Village_SerialNumber = val[1];
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableName`,
                    type: "POST",
                    data: {
                        Data: 'base_system_village_collective',
                        Condition: "VillageNumber = '" + val[1] + "'",
                    },
                    success: (data) => {

                        this.village_collective = data; // 直接使用 this
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            } else {
                this.dataForm.Village_SerialNumber = '';
            }

        },
        changeCity(val) {
            if (val.length > 0) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/clientOperation/getDistrictTree`,
                    type: "GET",
                    data: {
                        DistrictCode: val[2]
                    },
                    success: (data) => {
                        this.city1 = data[0].children;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        },
        getCity() {
            $.ajax({
                url: `${absolutepath()}api/Districtjl.json`,
                method: "get",
                success: (data) => {
                    data.forEach(item => {
                        item.children.forEach(ite => {
                            ite.children.forEach(it => {
                                it.children = undefined
                            })
                        })
                    })
                    this.city = data;
                }
            });
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: "POST",
                data: {
                    Data: 'blue_plate_house_room',
                    Condition: "Blue_Plate_SerialNumber = '" + this.Blue_Plate_SerialNumber + "'",
                },
                success: (data) => {
                    this.house_room = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                type: "POST",
                data: {
                    Data: 'blue_plate_house_unit',
                    Condition: "Blue_Plate_SerialNumber = '" + this.Blue_Plate_SerialNumber + "'",
                },
                success: (data) => {
                    this.house_unit = data; // 直接使用 this
                    data.filter(i => i.Row_Status != '1').forEach(item => {
                        this.dataForm1[Number(item.Unit_Number)] = item.Responsible_Person;
                    })
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.blue_plate = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'grid_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Grid = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.personal = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_house_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.house_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'relation_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.relation = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                if (this.dataForm.Self_Built_Houses == '1') {
                    this.dataForm.Layer_JSON = JSON.stringify(this.Layer);
                }
                //1.获取全部的
                let add = [], person = [], str = "", add1 = [];
                this.RoomNumbers.forEach(item => {
                    let obj = this.house_room.filter(i => i.Room_SerialNumber == item.Room_SerialNumber)[0]
                    if (obj) {
                        obj.Row_Status = '0';
                    } else {
                        add.push(item);
                    }
                });
                this.house_room.forEach(item => {
                    let obj = this.RoomNumbers.filter(i => i.Room_SerialNumber == item.Room_SerialNumber)[0]
                    if (!obj) {
                        item.Row_Status = '1';
                        if (item.Live_Status == '1') {
                            item.Live_Status = '0';
                            person.push(item.House_Room_SerialNumber);
                        }
                    }
                })
                this.UnitNumbers.forEach(item => {
                    let obj = this.house_unit.filter(i => i.Unit_Number == item.Unit_Number)[0]
                    if (obj) {
                        obj.Responsible_Person = item.Responsible_Person;
                        obj.Row_Status = '0';
                    } else {
                        add1.push(item);
                    }
                })
                this.house_unit.forEach(item => {
                    let obj = this.UnitNumbers.filter(i => i.Unit_Number == item.Unit_Number)[0]
                    if (!obj) {
                        item.Row_Status = '1';
                    }
                })
                let Modify = this.house_room.filter(i => i.Blue_Plate_SerialNumber_Original && i.Row_Status != i.Row_Status_Original);
                let Modify1 = this.house_unit.filter(i => i.Blue_Plate_SerialNumber_Original && (i.Row_Status != i.Row_Status_Original || i.Responsible_Person != i.Responsible_Person_Original));
                person.forEach(item => {
                    if (str) {
                        str += "\r\nOR House_Room_SerialNumber='" + item + "'"
                    } else {
                        str = "\r\nHouse_Room_SerialNumber='" + item + "'"
                    }
                })
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    type: "POST",
                    data: {
                        Data: 'blue_plate_person',
                        Condition: str ? "Row_Status='0' AND (" + str + ")" : "Row_Status='-'",
                    },
                    success: (data) => {
                        data.forEach(item => {
                            item.Row_Status = '1'
                        })
                        let lJSONObject_DataSet = {
                            Add: {
                                blue_plate_house_room: add,
                                blue_plate_house_unit: add1
                            },
                            Modify: {
                                blue_plate_person: data,
                                blue_plate_house_room: Modify,
                                blue_plate_information: [this.dataForm],
                                blue_plate_house_unit: Modify1
                            }
                        }
                        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                        if (!lJSONObject_DataSet['Add']) {
                            lJSONObject_DataSet['Add'] = new Object();
                        }
                        lJSONObject_DataSet['Add']['base_operation_log'] = [{
                            Operation_Type: '0',
                            Operation_Path: this.relativePath,
                            Operation_Main,
                            Operation_Interface: "generalSaveByRequestBody",
                            Operation_IP: getIpAddress()
                        }]
                        submitSave(lJSONObject_DataSet, (res) => {
                            if (res.Status == 'success') {
                                window.location.href = './index.html'
                            }
                            this.loading = false;
                        })
                        // $.ajax({
                        //     headers: requestHeaders("JSON"),
                        //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                        //     method: "post",
                        //     data: JSON.stringify({
                        //         Data: lJSONObject_DataSet,
                        //     }),
                        //     success: (res) => {
                        //         if (res.Status == 'success') {
                        //             window.location.href = './index.html'
                        //         }
                        //     },
                        //     complete: (XMLHttpRequest) => {
                        //         this.loading = false;
                        //         requestPretreatment(XMLHttpRequest);
                        //     }
                        // });
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            }
        },
        handleRoom(Unit_Number, Layer_Number, Room_Number) {
            let row = this.house_room.filter(i => i.Unit_Number == Unit_Number && i.Layer_Number == Layer_Number && i.Room_Number == Room_Number)[0];
            if (this.dataForm.Self_Built_Houses == '0') {
                if (row.Live_Status == '1') {
                    window.location.href = 'add1.html?action=Modify&Blue_Plate_SerialNumber=' + row.Blue_Plate_SerialNumber
                        + "&House_Room_SerialNumber=" + row.House_Room_SerialNumber;
                } else {
                    window.location.href = 'add1.html?action=Add&Blue_Plate_SerialNumber=' + row.Blue_Plate_SerialNumber
                        + "&House_Room_SerialNumber=" + row.House_Room_SerialNumber;
                }
            }
        }
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-text-editor', ylTextEditor);
app.component('yl-house-grid', ylHouseGrid);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
